//
//  HomeTravelInformationViewController.m
//  CivilianTravel
//
//  Created by 许仕杰 on 16/7/23.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "HomeTravelInformationViewController.h"
#import "CollectionModel.h"
#import "CoreDataManager.h"
@interface HomeTravelInformationViewController ()<UIWebViewDelegate>
@property (nonatomic,strong)UIWebView *webview;


/** 姓名 */
@property (nonatomic, strong) UILabel *name;
/** 标题 */
@property (nonatomic, strong) UILabel *titleName;
/** 观看人数 */
@property (nonatomic, strong) UILabel *views;
/** 滑动图 */
@property (nonatomic, strong) UIScrollView *scroller;
/** html */
@property (nonatomic, strong) NSString *html;
@end

@implementation HomeTravelInformationViewController

- (UILabel *)views{
    if (!_views) {
        _views = [[UILabel alloc] initWithFrame:CGRectMake(kWidth-150, 34, 150, 34)];
    }
    return _views;
}

- (UILabel *)titleName{
    if (!_titleName) {
        _titleName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
    }
    return _titleName;
}

- (UILabel *)name{
    if (!_name) {
        _name = [[UILabel alloc] initWithFrame:CGRectMake(0, 34, kWidth-150, 30)];
    }
    return _name;
}

- (UIScrollView *)scroller{
    if (!_scroller) {
        _scroller = [[UIScrollView alloc] initWithFrame:self.view.frame];
    }
    return _scroller;
}



- (UIWebView *)webview{
    if (!_webview) {
        _webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight)];
        _webview.delegate = self;
    }
    return _webview;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scroller];
    [self requestManagerWithYid:self.yid];
    [self.scroller addSubview:self.name];
    [self.scroller addSubview:self.titleName];
    [self.scroller addSubview:self.webview];
    [self.scroller addSubview:self.views];
    self.webview.userInteractionEnabled = NO;
}

//收藏
- (void)collectAction{
    if ([[CoreDataManager defaultManager] isContainsReadWithtitle:self.titleName.text]) {
        [[CoreDataManager defaultManager] deleteCollectionModelWithtitle:self.titleName.text];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction)];
    } else{
        [[CoreDataManager defaultManager] insertCollectionModelWithCollectionHtml:self.html title:self.titleName.text name:self.name.text views:self.views.text];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction)];
    }
}

//请求数据
- (void)requestManagerWithYid:(NSString *)yid{
    [RequestManager fetchDataWithUrl:@"http://www.roadqu.com/api/mobile/qunawan/poi/youji"parameter:@{@"a_t":@"1469241365000",@"token":@"578624ce19a3f",@"yid":yid,@"sign":@"9ec97128872c715f62b20df3542658209201d11a"} method:@"POST" compeleted:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        self.html = dic[@"data"][@"content"];
        [self.webview loadHTMLString:dic[@"data"][@"content"] baseURL:nil];
        self.name.text = [NSString stringWithFormat:@"标题:%@",dic[@"data"][@"author"]];
        self.titleName.text = [NSString stringWithFormat:@"作者:%@",dic[@"data"][@"title"]];
        self.views.text = [NSString stringWithFormat:@"浏览人数:%@",dic[@"data"][@"views"]];
    } updateUI:^{
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark ---webViewDelegate-----
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
    CGRect webViewFrame = webView.frame;
    webViewFrame.size.height = height;
    webView.frame = webViewFrame;
    self.scroller.contentSize = CGSizeMake(kWidth, CGRectGetMaxY(self.webview.frame));
    if ([[CoreDataManager defaultManager] isContainsReadWithtitle:self.titleName.text]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction)];
    } else {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction)];
    }

}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"开始加载");
}

@end
